package bavaria.hightech.banking.accounts;

import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.NumberFormat;

import bavaria.hightech.banking.conditions.GiroConditions;
import bavaria.hightech.banking.exceptions.FixedDepositException;
import bavaria.hightech.banking.money.Currency.currencyEnum;
import bavaria.hightech.banking.resourceBundles.LanguageBundle;
import bavaria.hightech.banking.time.TimeServer;

/**
 * extends class account
 * 
 * @author Dietmar 
 */
public class GiroAccount extends Account {

	private GiroConditions act_condition;

	/**
	 * Default-Konstruktor giro account with acc nr, depositor and conditions
	 * 
	 * @param accNr
	 * @param depositor
	 * @param interest
	 */
	public GiroAccount(int accNr, String depositor,
			GiroConditions act_condition, currencyEnum currency,
			LanguageBundle languageBundle) {
		super(accNr, depositor, currency);
		this.act_condition = act_condition;
		this.languageBundle = languageBundle;
	}

	/**
	 * draws money and calls the booking function
	 * 
	 * @param amount
	 * @param reason
	 * @throws FixedDepositException
	 */
	@Override
	public boolean drawAmount(currencyEnum currency, long amount, String reason)
			throws FixedDepositException {
		long drawable = getBalance() + act_condition.getOverdraft();
		if (drawable - amount >= 0) {
			super.drawAmount(currency, amount, reason);
			return true;
		} else {
			return false;
		}
	}

	/**
	 * calculates interest depending on positive/negative balance
	 * 
	 * @throws FixedDepositException
	 */
	@Override
	@SuppressWarnings("static-access")
	public void applyInterest() throws FixedDepositException {
		if (TimeServer.getCalendar().DAY_OF_MONTH == 1) {
			return;
		}
		if (getBalance() > 0) {
			addAmount(money.act_currency,
					(long) (getBalance() / 100 * getInterest_Have()),
					"interests");
		} else if (getBalance() < 0) {
			drawAmount(money.act_currency,
					(long) (getBalance() / 100 * getInterest_Is()), "interests");
		}
	}

	@Override
	public String toString() {
		String str = "";
		String formattedBalance = NumberFormat.getNumberInstance(
				languageBundle.getLocale()).format(getBalance());
		String formattedDate = DateFormat.getDateInstance(DateFormat.MEDIUM,
				languageBundle.getLocale()).format(creationTimeStamp.getTime());
		str = "{0}: {1}\n";
		str += "{3}: " + formattedDate + "\n";
		str += "{4}: " + getDepositor() + "\n";
		str += "{18}: " + getAccountNumber() + "\n";
		str += "{5}: " + formattedBalance + "\n";
		str += act_condition;
		str = MessageFormat.format(str, languageBundle.getContents());
		return str;
	}

	/**
	 * get interest is
	 * @return
	 */
	public float getInterest_Is() {
		return act_condition.getInterest_Is();
	}

	/**
	 * get interest have
	 * @return
	 */
	public float getInterest_Have() {
		return act_condition.getInterest_Have();
	}

	/**
	 * get condition
	 * @return
	 */
	public GiroConditions getCondition() {
		return act_condition;
	}
}
